package com.sms.user;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.*;

public class GetStudentServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String stutype = request.getParameter("stutype");
		if(stutype == null) {//非人员处理
			List<Student> list = new ArrayList<Student>();
			list = User.getAllStudents();
			if (list != null && list.size() > 0) {
				JSONArray json = new JSONArray();
				List jlist = new ArrayList();
				for (int i = 0; i < list.size(); i++) {
					Student s = (Student) list.get(i);
					String sname = s.getSname();
					JSONObject j = new JSONObject();
					j.put("sno", s.getSno());
					j.put("sname", sname);
					json.add(j);
				}
				out.println(json);
			} 
		} else {
			List<Student> list = new ArrayList<Student>();
			list = User.getAllStudents(stutype);
			if (list != null && list.size() > 0) {
				JSONArray json = new JSONArray();
				List jlist = new ArrayList();
				for (int i = 0; i < list.size(); i++) {
					Student s = (Student) list.get(i);
					String sname = s.getSname();
					JSONObject j = new JSONObject();
					j.put("sno", s.getSno());
					j.put("sname", sname);
					j.put("sex", s.getSex());
					j.put("major", s.getMajor());
					j.put("teacher", s.getTname());
					j.put("type", s.getType());
					j.put("number", s.getItemCount());
					json.add(j);
				}
				out.println(json);
			} 
		}
		
		
		
		out.flush();
		out.close();
	}

}
